﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrangePrj
{
    [Serializable]
    public class ChartData
    {
        public List<double> XData { get; set; }
        public List<double> YData { get; set; }
        private int Total
        {
            get
            {
                int val=0;
                foreach (int i in YData)
                {
                    val += i;
                }
                return val; 
            }
        }

        public ChartData()
        {
            XData = new List<double>();
            YData = new List<double>();
        }
        public override string ToString()
        {
            this.Sort();
            string output = "";
            for (int i = 0; i < XData.Count; i++)
            {
                output += XData[i].ToString() + " - " + YData[i]*100 / Total + "% - "+ YData[i]+"\n";
            }
            output += "Iterations : " + Total.ToString();
            return output;
        }        
        internal void AddElement(double d)
        {
            bool exist = false;
            for (int i = 0; i < XData.Count; i++)
            {
                if (XData[i] == d)
                {
                    YData[i]++;
                    exist = true;
                }
            }
            if (!exist)
            {
                XData.Add(d);
                YData.Add(1);
            }
        }
        internal void Sort()
        {
            double[] Xtmp = new double[XData.Count];
            double[] Ytmp = new double[YData.Count];
            XData.CopyTo(Xtmp);
            YData.CopyTo(Ytmp);
            XData.Sort();
            for (int i = 0; i < XData.Count; i++)
            {
                for (int j = 0; j < Xtmp.Length; j++)
                {
                    if (XData[i] == Xtmp[j])
                    {
                        YData[i] = Ytmp[j];
                    }
                }
            }
        }
        internal int GetMaxYid()
        {
            double res = 0;
            foreach (double d in YData)
            {
                if (res < d)
                    res = d;
            }
            return YData.FindIndex(val => val == res);
        }
    }
}
